<?php

class Controller_Pc_Api_Standard_Index extends Controller_Pc_Api_Base
{

    public function before()
    {
        parent::before();
    }

    // 首页导航
    public function action_index_nav()
    {
        $typeids = St_Filter::remove_xss($this->request_body->content->typeids);

        $result = DB::select()->from('m_nav')->where('m_isopen','=','1')->and_where(DB::expr(" and m_typeid in({$typeids})"))->order_by('m_displayorder','asc')->execute()->as_array();
        foreach ($result as &$v)
        {
            if($v['m_typeid'] == 4)
            {
                $v['m_url'] = '/article/';
            }
            if($v['m_typeid'] == 101)
            {
                $v['m_url'] = '/travelNotes/';
            }
            $v['m_ico'] = $v['m_ico'] ? Common::img($v['m_ico']) : '';
        }
        $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);
    }

    //热门攻略
    public function action_index_hot_raiders()
    {
        $result = Taglib_Article::query(array('flag'=>'order','row'=>3));
        foreach($result as &$item){
            $item['litpic'] = Common::img($item['litpic']);
        }
        $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);
    }

    // 热门游记
    public function action_index_hot_notes()
    {
        $sql = "SELECT a.*,a.shownum+a.read_num nums FROM `sline_notes` a LEFT JOIN `sline_allorderlist` b ON (a.id=b.aid and b.typeid=101) WHERE a.id is not null AND a.status=1 ORDER BY nums DESC,IFNULL(b.displayorder,9999) asc,a.shownum desc,a.modtime desc,a.addtime DESC LIMIT 0,3";
        $result = DB::query(Database::SELECT, $sql)->execute()->as_array();
        foreach($result as &$item){
            $item['litpic'] = Common::img($item['litpic']);
            $member = DB::select('nickname','litpic')->from('member')->where('mid','=',$item['memberid'])->execute()->current();
            $item['member']['litpic'] = Common::img($member['litpic']);
            $item['member']['nickname'] = $member['nickname'];
        }
        $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);
    }

    // 热门产品
    public function action_index_hot_list()
    {
        // 首页热门产品不再显示攻略和游记
        $typeids = St_Filter::remove_xss($this->request_body->content->typeids);
        $result = DB::select()->from('m_nav')->where('m_isopen','=','1')->and_where(DB::expr(" and m_typeid in({$typeids}) and m_typeid<>106"))->order_by('m_displayorder','asc')->limit(4)->execute()->as_array();
        foreach ($result as &$v)
        {
            $pinyin = Model_Model::all_model($v['m_typeid'],'pinyin');
            $pinyin = $pinyin == 'ship_line' ? 'ship' : $pinyin;
            if(!empty($pinyin))
            {
                $tag = 'Taglib_'.ucfirst($pinyin);
                $res = $tag::query(array('flag' => 'order', 'row' => 10));
                foreach ($res as &$item)
                {
                    $item['litpic'] = Common::img($item['litpic'],340,230);
                }
                $v['list'] = $res;
            }
        }

        $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);
    }


    // 小程序新自定义导航
    public function action_xcx_index_nav()
    {
        $siteId = Common::remove_xss($this->request_body->content->siteId);
        $secret = Common::remove_xss($this->request_body->content->secret);
        $mini_type = $this->request_body->content->mini_type; // 1 三方平台小程序(需要检查是否是组合小程序) 2 独立小程序

        // 检查小程序是否有自定义导航
        $nav = DB::select()->from("xcx_nav")->where("m_isopen","=",1)->order_by("m_displayorder","ASC")->execute()->as_array();
        // 获取小程序权限
        $auth = Model_Api_Standard_System::get_xcx_auth($mini_type,$siteId,$secret);
        $default_arr = array("canvas","tongyong");
        if(!empty($nav))
        {
            $type = 1;
            foreach ($nav as $k=>&$v)
            {
                $v['m_ico'] = $v['m_ico'] ? Common::img($v['m_ico']) : '';
                if(!in_array($v['m_mode'],$default_arr)&& !in_array($v['m_mode'],$auth))
                {
                    unset($nav[$k]);
                }
            }
        }
        else
        {
            $type = 2;
            $typeids = St_Filter::remove_xss($this->request_body->content->typeids);
            $nav = DB::select()->from('m_nav')->where('m_isopen','=','1')->and_where(DB::expr(" and m_typeid in({$typeids})"))->order_by('m_displayorder','asc')->execute()->as_array();
            foreach ($nav as &$v)
            {
                if($v['m_typeid'] == 4)
                {
                    $v['m_url'] = '/article/';
                }
                if($v['m_typeid'] == 101)
                {
                    $v['m_url'] = '/travelNotes/';
                }
                $v['m_ico'] = $v['m_ico'] ? Common::img($v['m_ico']) : '';
            }
        }

        $result = array(
            "status"=> true,
            "list"  => array_values($nav),
            "type"  => $type // 1 自定义导航 2默认导航
        );
        $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);
    }
}